package inventory.service.impl;

import inventory.entity.MaterialCategory;
import inventory.entity.MaterialTypical;
import inventory.entity.po.TerminalAndEquipmentPo;
import inventory.mappers.*;
import inventory.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    MaterialCategoryMapper materialCategoryMapper;

    @Autowired
    MaterialTypicalMapper materialTypicalMapper;

    @Autowired
    RoleMapper roleMapper;

    @Override
    public MaterialCategory selectMaterialCategory(String categoryId) {
        return materialCategoryMapper.selectById(categoryId);
    }

    @Override
    public List queryCity() {
        return roleMapper.queryAllCity();
    }

    @Override
    public List queryCounty() {
        return roleMapper.queryAllCounty();
    }

    @Transactional
    @Override
    public void addMaterialType(MaterialTypical materialTypical) {
        materialTypicalMapper.insert(materialTypical);
    }

    @Override
    public boolean deleteMaterialTypeById(String typicalId) {
        List<MaterialTypical> cannnotDelete = materialTypicalMapper.selectChildById(typicalId);
        if (cannnotDelete==null) {
            materialTypicalMapper.deleteById(typicalId);
            return true;
        }else{
            return false;
        }
    }

    @Transactional
    @Override
    public void modifyMaterialTypeById(MaterialTypical materialTypical) {
        materialTypicalMapper.updateById(materialTypical);
    }

    @Override
    public MaterialTypical selectMaterialType(String typicalId) {
        return materialTypicalMapper.selectById(typicalId);
    }

    @Override
    public boolean deleteMaterialByArray(List<String> typesId) {
        List<MaterialTypical> list = materialTypicalMapper.selectListChildByTypesId(typesId);
        if (list==null) {
            materialTypicalMapper.deleteMaterialByArray(typesId);
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<TerminalAndEquipmentPo> queryTypeByTypicalName(String typicalName, String managerId) {
        return materialTypicalMapper.selectTypicalByTypicalName(typicalName,managerId);
    }

    @Override
    public List<MaterialTypical> queryTypeByManagerId(String managerId) {
        return materialTypicalMapper.selectByManagerId(managerId);
    }


}
